package top.caoayu.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

/**
 * @Author: caoxinyu
 * @Date: 2023/8/25-15:03
 */
@EnableAspectJAutoProxy
@Component
@Aspect
public class AopDemoAspect {
    /*
    * 定义切入点
    * */
    @Pointcut("execution(* top.caoayu.service.impl.*.*(..))")
    public void pointCutMethod(){

    }
    /*
    * 环绕
    * */
    @Around("pointCutMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕通知: 进入方法");
        Object proceed = pjp.proceed();
        System.out.println("环绕通知: 退出方法");
        return proceed;
    }
    /*
    * 前置
    * */
    @Before("pointCutMethod()")
    public void doBefore(){
        System.out.println("前置通知");
    }
    /**
     * 后置通知.
     *
     * @param result return val
     */
    @AfterReturning(value = "pointCutMethod()",returning = "result")
    public void doAfterReturning(String result) {
        System.out.println("后置通知, 返回值: " + result);
    }

    /**
     * 异常通知.
     *
     * @param e exception
     */
    @AfterThrowing(value = "pointCutMethod()",throwing = "e")
    public void doAfterThrowing(Exception e) {
        System.out.println("异常通知, 异常: " + e.getMessage());
    }

    /**
     * 最终通知.
     */
    @After("pointCutMethod()")
    public void doAfter() {
        System.out.println("最终通知");
    }

}
